<template>
  <div class="WaterfallPage">
    <!-- 搜索框 -->
    <div class="search-bar">
      <a-input-search
        v-model:value="searchParams.searchText"
        placeholder="从海量图片中搜索"
        enter-button="搜索"
        size="large"
        @search="doSearch"
      />
    </div>
    <!-- 分类和标签筛选 -->
    <a-tabs v-model:active-key="selectedCategory" @change="doSearch">
      <a-tab-pane key="all" tab="全部" />
      <a-tab-pane v-for="category in categoryList" :tab="category" :key="category"
                  @click="doRecordCategory(category)" />
    </a-tabs>
    <div class="tag-bar">
      <span style="margin-right: 8px">标签：</span>
      <a-space :size="[0, 8]" wrap>
        <a-checkable-tag
          v-for="(tag, index) in tagList"
          :key="tag"
          v-model:checked="selectedTagList[index]"
          @change="handleTags(tag)"
        >
          {{ tag }}
        </a-checkable-tag>
      </a-space>
    </div>
    <WaterFallPictureList class="waterfallPictureList" :data-list="dataList" :breakpoints="breakpoints"></WaterFallPictureList>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { LazyImg, Waterfall } from "vue-waterfall-plugin-next";
import "vue-waterfall-plugin-next/dist/style.css";
import WaterFallPictureList from '@/components/WaterFallPictureList.vue'
import {
  listAllPictureUsingPost,
  listPictureTagCategoryUsingGet,
  listPictureVoByPageUsingPost
} from '@/api/pictureController.ts'
import { message } from 'ant-design-vue'
import { recordTagsUsingPost } from '@/api/tagsController.ts'
import { recordCategoriesUsingPost } from '@/api/categoriesController.ts'


const dataList = ref<API.Picture[]>([])
// 标签和分类列表
const categoryList = ref<string[]>([])
const selectedCategory = ref<string>('all')
const tagList = ref<string[]>([])
const selectedTagList = ref<boolean[]>([])

//根据屏幕宽度动态调整瀑布流的列数
const breakpoints = {
  1200: { rowPerView: 3 },
  800: { rowPerView: 2 },
  500: { rowPerView: 1 },
}

// 搜索条件
const searchParams = reactive<API.PictureQueryRequest>({
  sortField: 'createTime',
  sortOrder: 'descend'
})

// 获取数据
const fetchData = async () => {
  // 转换搜索参数
  const params = {
    fields: ['id', 'name', 'url', 'viewCount', 'likeCount'],
    ...searchParams,
    tags: [] as string[]
  }
  if (selectedCategory.value !== 'all') {
    params.category = selectedCategory.value
  }
  // [true, false, false] => ['java']
  selectedTagList.value.forEach((useTag, index) => {
    if (useTag) {
      params.tags.push(tagList.value[index])
    }
  })
  const res = await listAllPictureUsingPost(params)
  if (res.data.code === 0 && res.data.data) {
    dataList.value = res.data.data ?? []
  } else {
    message.error('获取数据失败，' + res.data.message)
  }
}

// 页面加载时获取数据，请求一次
onMounted(() => {
  fetchData()
})

const doSearch = () => {
  // 重置搜索条件
  fetchData()
}

/**
 * 处理标签
 * @param tag
 */
const handleTags = (tag: string) => {
  doSearch()
  doRecordTag(tag)
}

// 搜索
const doRecordTag = async (tag: any) => {
  console.log(tag)
  const params = {
    tagName: tag
  }
  const res = await recordTagsUsingPost(params)
  if (res.data.code === 0 && res.data.data) {
    console.log('recordTagsUsingPost success')
  } else {
    message.error('获取数据失败，' + res.data.message)
  }
}

/**
 * 处理分类
 */
const handleCategories = (category: string) => {
  doSearch()
  doRecordCategory(category)
}

const doRecordCategory = async (category: any) => {
  console.log(category)
  const params = {
    categoryName: category
  }
  const res = await recordCategoriesUsingPost(params)
  if (res.data.code === 0 && res.data.data) {
    console.log('recordCategoriesUsingPost success')
  } else {
    message.error('获取数据失败，' + res.data.message)
  }
}

/**
 * 获取标签和分类选项
 * @param values
 */
const getTagCategoryOptions = async () => {
  const res = await listPictureTagCategoryUsingGet()
  if (res.data.code === 0 && res.data.data) {
    tagList.value = res.data.data.tagList ?? []
    categoryList.value = res.data.data.categoryList ?? []
  } else {
    message.error('获取标签分类列表失败，' + res.data.message)
  }
}

onMounted(() => {
  getTagCategoryOptions()
})
</script>

<style scoped>
.waterfallPictureList{
  margin-top: 16px;
}
</style>
